//
//  XKLRecommendTagsTableViewController.m
//  GuiZiShouDuanZi
//
//  Created by xmg on 16/4/27.
//  Copyright © 2016年 xiao_kl. All rights reserved.
//

#import "XKLRecommendTagsTableViewController.h"
#import <AFNetworking.h>
#import <SVProgressHUD.h>
#import <MJExtension.h>
#import "XKLRecommendTagItem.h"
#import "XKLRecommendTagCell.h"


@interface XKLRecommendTagsTableViewController ()
/**模型数组*/
@property (nonatomic,strong)NSArray *items;

@property (nonatomic,weak)AFHTTPSessionManager *manager;
@end

@implementation XKLRecommendTagsTableViewController

-(AFHTTPSessionManager *)manager{
    if (_manager==nil) {
        //请求管理者是有强引用的,所以不需要我们引用
        _manager=[AFHTTPSessionManager manager];
    }
    return _manager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self upTableView];
    
    
    [self updata];
    
}

static NSString * const cellID=@"recommendTag";
-(void)upTableView{
    self.view.backgroundColor=XKLColor(215,215,215);
    //遮盖
    [SVProgressHUD show];
    
    self.tableView.rowHeight=71;
    //注册
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XKLRecommendTagCell class]) bundle:nil] forCellReuseIdentifier:cellID];
}

-(void)updata{
    
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    dic[@"a"]=@"tag_recommend";
    dic[@"c"]=@"topic";
    dic[@"action"]=@"sub";
    //发送请求
    [self.manager GET:@"http://api.budejie.com/api/api_open.php" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.items = [XKLRecommendTagItem mj_objectArrayWithKeyValuesArray:responseObject];
        
        [self.tableView reloadData];
        
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
        [SVProgressHUD showErrorWithStatus:@"加载失败"];
    }];
}

//控制器不显示的时候取消指示器和取消请求
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //取消指示器
    [SVProgressHUD dismiss];
    //取消请求
    [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.items.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XKLRecommendTagCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    XKLRecommendTagItem *item=self.items[indexPath.row];
    cell.item=item;
    
    return cell;
}



@end
